package com.heu.blood.autologous.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.autologous.dao.BloodApplicationAutologousDao;
import com.heu.blood.autologous.dao.BloodApplicationItemAutologousDao;
import com.heu.blood.autologous.dao.BloodInventoryAutologousDao;
import com.heu.blood.autologous.dictenum.ApplicationStateAutologousEnum;
import com.heu.blood.autologous.dictenum.InventoryStateAutologousEnum;
import com.heu.blood.autologous.entity.BloodApplicationAutologousEntity;
import com.heu.blood.autologous.entity.BloodApplicationItemAutologousEntity;
import com.heu.blood.autologous.entity.BloodInventoryAutologousEntity;
import com.heu.blood.autologous.service.BloodApplicationItemAutologousService;
import com.heu.blood.common.Constant.ConstantData;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("bloodApplicationItemAutologousService")
public class BloodApplicationItemAutologousServiceImpl extends ServiceImpl<BloodApplicationItemAutologousDao, BloodApplicationItemAutologousEntity> implements BloodApplicationItemAutologousService {

    @Autowired
    BloodApplicationItemAutologousDao bloodApplicationItemAutologousDao;

    @Autowired
    BloodApplicationAutologousDao bloodApplicationAutologousDao;

    @Autowired
    BloodInventoryAutologousDao bloodInventoryAutologousDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodApplicationItemAutologousEntity> page = this.page(
                new Query<BloodApplicationItemAutologousEntity>().getPage(params),
                new QueryWrapper<BloodApplicationItemAutologousEntity>()
        );

        return new PageUtils(page);
    }

    /*@Override
    public int addInventory(BloodApplicationItemAutologousEntity bloodApplicationItemAutologous) {

        bloodApplicationItemAutologous.setCollectState(ApplicationStateAutologousEnum.BLOOD_COLLECTED.code());
      //  bloodApplicationItemAutologous.setIfCollected(ItemCollectStateEnum.AUTO_COLLECTED_ALREADY.value());
        bloodApplicationItemAutologousDao.updateById(bloodApplicationItemAutologous);

        String applicationId = bloodApplicationItemAutologous.getApplicationId();

        LambdaQueryWrapper<BloodApplicationAutologousEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodApplicationAutologousEntity::getApplicationId,applicationId);
        BloodApplicationAutologousEntity bloodApplicationAutologousEntity = bloodApplicationAutologousDao.selectOne(queryWrapper);

        // 检查库存中是否已存在该申请项的记录
        LambdaQueryWrapper<BloodInventoryAutologousEntity> inventoryCheckWrapper = new LambdaQueryWrapper<>();
        inventoryCheckWrapper.eq(BloodInventoryAutologousEntity::getBloodApplicationItemId, bloodApplicationItemAutologous.getId());
        BloodInventoryAutologousEntity existingRecord = bloodInventoryAutologousDao.selectOne(inventoryCheckWrapper);

        if (existingRecord == null) {

            BloodInventoryAutologousEntity bloodInventoryAutologousEntity = new BloodInventoryAutologousEntity();

            BeanUtils.copyProperties(bloodApplicationAutologousEntity, bloodInventoryAutologousEntity);

            GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(ConstantData.AUTOLOGOUS_BLOOD_NO_WORK_ID, ConstantData.AUTOLOGOUS_BLOOD_NO_PREFIX);
            String bloodId = generateNumberBySnowFlake.genNumberByflag();

            bloodInventoryAutologousEntity.setBloodId(bloodId);
            bloodInventoryAutologousEntity.setGmtCreate(new Date());
            bloodInventoryAutologousEntity.setGmtModified(new Date());
            bloodInventoryAutologousEntity.setTransfusionTime(bloodApplicationItemAutologous.getTransfusionTime());
            bloodInventoryAutologousEntity.setTransferStatus(InventoryStateAutologousEnum.AUTO_PENDING_RECEIPT.code());
            bloodInventoryAutologousEntity.setOperatorId("");
            bloodInventoryAutologousEntity.setOperatorName("");
            bloodInventoryAutologousEntity.setBloodType(bloodApplicationItemAutologous.getBloodType());
            bloodInventoryAutologousEntity.setBloodCount(bloodApplicationItemAutologous.getApplyCount());
            bloodInventoryAutologousEntity.setMeasurementUnit(bloodApplicationItemAutologous.getMeasurementUnit());
            bloodInventoryAutologousEntity.setBloodApplicationItemId(bloodApplicationItemAutologous.getId());


            //顺便判断申请单下的申请项是否全部已采血
            // 在这获取所有的申请单审批信息
            LambdaQueryWrapper<BloodApplicationItemAutologousEntity> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Strings.isNotBlank(applicationId), BloodApplicationItemAutologousEntity::getApplicationId, applicationId);
            List<BloodApplicationItemAutologousEntity> bloodApplicationItemAutologousEntityList = bloodApplicationItemAutologousDao.selectList(queryWrapper1);

            int f = 0;
            for (BloodApplicationItemAutologousEntity b : bloodApplicationItemAutologousEntityList) {
                String collectState = b.getCollectState();
                if (!ApplicationStateAutologousEnum.BLOOD_COLLECTED.code().equals(collectState)) {
                    f = 1;
                    break;
                }
            }


            if (f == 0) {
                bloodApplicationAutologousEntity.setApplicationState(ApplicationStateAutologousEnum.BLOOD_COLLECTED.code());
                bloodApplicationAutologousDao.updateById(bloodApplicationAutologousEntity);
            }


            int i = bloodInventoryAutologousDao.insert(bloodInventoryAutologousEntity);

            return i;
        }
        return 0;
    }*/

}